package cn.wolfcode.p2p.web.controller;

import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.BidConst;
import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.service.IBidRequestService;
import cn.wolfcode.p2p.web.util.RequiredLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 前台:借款信息
 */
@Controller
public class BorrowApplyController {
    @Autowired
    private IBidRequestService bidRequestService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IAccountService accountService;

    @RequestMapping("/borrowInfo")
    @RequiredLogin
    public String borrowInfo(Model model) {
        UserInfo current = userInfoService.getCurrent();
        if (!current.getBorrowable()) {
            // 1.当前不满足借款条件
            return "redirect:/borrow";
        } else if (current.getHasRequestInProcess()) {
            return "borrow_apply_result";
        }
        model.addAttribute("userInfo", current);
        model.addAttribute("account", accountService.getCurrent()); // 当前信用额度
        model.addAttribute("minBidRequestAmount", BidConst.MIN_BIDREQUEST_AMOUNT); // 最下借款金额
        model.addAttribute("minBidAmount", BidConst.MIN_BID_AMOUNT); // 最小投标金额
        return "borrow_apply";


//        // 1.获取当前用户,判断是否有正在进行申请,有则显示正在申请流程中
//        if (current.getHasBorrowAply()) {
//            return "borrow_apply_result";
//        }
//        // 2.没有在申请的显示申请页面
//        model.addAttribute("userInfo", current);
//        model.addAttribute("account", accountService.getCurrent());
//        model.addAttribute("minBidRequestAmount", BidConst.MIN_BIDREQUEST_AMOUNT);
//        model.addAttribute("minBidAmount", BidConst.MIN_BID_AMOUNT);
//        return "borrow_apply";
    }

    // 借款信息
    @RequestMapping("/borrow_apply")
    @RequiredLogin
    public String applyBorrow(BidRequest vo) {
        bidRequestService.apply(vo);
        return "redirect:/borrowInfo";
    }
}
